var CaseSelectionManagement = {
    id: "caseSelectionManagementTable",
    seItem: null,
    table: null,
    layerIndex: -1
};
CaseSelectionManagement.initColumn = function () {
    var columns =
        [
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '病例类型', field: 'caseType', align: 'center', valign: 'middle', width: 110, sortable: true},
            {
                title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 110,
                formatter: function (value, row, index) {
                    if(row.customerAge == "" || row.customerAge == null || row.customerAge == "null"){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerAge >= 0 && row.customerAge < 18) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge >= 18 && row.customerAge <= 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge > 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    }
                }
            },
            {
                title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 80, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男";
                    } else if (value == 2) {
                        return "女"
                    } else {
                        return "未知"
                    }
                }
            },
            {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
            {title: '申请人', field: 'applyer', align: 'center', valign: 'middle', sortable: true, width: 80},
            {
                title: '申请时间', field: 'applyTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {
                title: '评选时间', field: 'selectionTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {title: '得分', field: 'caseScore', align: 'center', valign: 'middle', sortable: true, width: 80},
            {title: '得票', field: 'votes', align: 'center', valign: 'middle', sortable: true, width: 80},
        ];
    return columns;
};

/**
 * 查询预约客户列表
 */
CaseSelectionManagement.search = function () {
    CaseSelectionManagement.table.refresh({query: CaseSelectionManagement.formParams()});
    CaseSelectionManagement.total();
};

//统计人数
CaseSelectionManagement.total = function () {
    $("#distributionWaiting").addClass('actionClick').siblings().removeClass('actionClick');
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/caseSelectionManagement/caseSelectionManagementTotal',
        data: CaseSelectionManagement.formParams(),
        dataType: "json",
        success: function (data) {
            $("#daitj").html(data[0].countNum);
        }
    });
};


/**
 * 新市场预约表单查询提交参数
 */
CaseSelectionManagement.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#caseSelectionPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    return queryData;
};

/**
 * 把对象拼接成URL的键值对参数
 * param1=value1&param2=value2
 */
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

CaseSelectionManagement.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CaseSelectionManagement.seItem = selected[0];
        return true;
    }
};

/**
 * 重置
 */
CaseSelectionManagement.resetSearch = function () {
    $("#caseSelectionPublic", parent.document).val("");
    $("#startTime").val("");
    $("#endTime").val("");
    CaseSelectionManagement.search();
};

/**
 * 分配病例
 */
CaseSelectionManagement.allocationCase = function () {
    $.ajax({
        type: "POST",
        url: Feng.ctxPath + '/caseSelectionUpload/allocationCases',
        success: function (data) {
            console.log(data);
            Feng.success("分配病例成功");
        },
        error:function (data) {alert("没有权限，请联系系统管理员！");}
    });
};

CaseSelectionManagement.scoreCase = function () {
    $.ajax({
        type: "POST",
        url: Feng.ctxPath + '/caseSelectionUpload/scoreCase',
        success: function (data) {
            Feng.success("分配打分成功");
            CaseSelectionManagement.search();
        },
        error:function (data) {alert("没有权限，请联系系统管理员！");}
    });
};

/**
 * 主页搜索
 */
$("#caseSelectionSearchAll", parent.document).click(function () {
    CaseSelectionManagement.search();
});

/**
 * 初始化赋值时间
 */
CaseSelectionManagement.time = function () {
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    var dates = new Date();
    dates.setDate(dates.getDate() - 1);
    var currentYears = dates.getFullYear();
    var currentMonths = dates.getMonth() + 1;
    var currentDays = dates.getDate();
    if (currentMonths < 10) currentMonths = "0" + currentMonths;
    if (currentDays < 10) currentDays = "0" + currentDays;
    $("#startTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00");
    $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
};
var scollPostion = 0;
/**
 * 获取表格滚动到什么地方
 */
CaseSelectionManagement.getScrollPosition = function () {
    scollPostion = $('#CaseSelectionManagement').bootstrapTable('getScrollPosition');
};
/**
 * 指定表格滚动到什么地方
 */
CaseSelectionManagement.scrollTo = function () {
    $("#CaseSelectionManagement").on('load-success.bs.table', function (data) {
        $('#CaseSelectionManagement').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    var defaultColunms = CaseSelectionManagement.initColumn();
    var table = new BSTable(CaseSelectionManagement.id, "/caseSelectionManagement/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CaseSelectionManagement.formParams());
    CaseSelectionManagement.table = table.init();
    CaseSelectionManagement.search();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {CaseSelectionManagement.search();});
    checkDate = function () {CaseSelectionManagement.search();};
    callbackTable = function () {CaseSelectionManagement.search();};
});